package com.yitiao.data_algorithms.leetcode.editor.cn;

import java.util.HashMap;
import java.util.Map;

class LongestSubstringWithoutRepeatingCharacters {
    // 2024-03-12 15:51:28
    public static void main(String[] args) {
        Solution solution = new LongestSubstringWithoutRepeatingCharacters().new Solution();
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        /**
         * abcabcbb
         */
        public int lengthOfLongestSubstring(String s) {
            if (s.length() == 1) {
                return 1;
            } else if (s.isEmpty()) {
                return 0;
            }
            Map<Character, Integer> map = new HashMap<>();
            int left = 0;
            int right = 0;
            int max = 0;
            while (right < s.length()) {
                if (map.containsKey(s.charAt(right))) {
                    left = Math.max(left, map.get(s.charAt(right)) + 1);
                }
                max = Math.max(max, right - left + 1);
                map.put(s.charAt(right), right);
                right++;
            }
            return max;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}